package edu.bw.web.user;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.dto.InsertUserForm;
import edu.bw.service.UserService;
import edu.bw.service.impl.UserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/user/insert")
public class InsertUserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         //{"userId":0,"username":"ceshi1234","password":"12345678","name":"测试","sex":"男","tel":"17638765433"
        //   ,"email":"17638765433@qq.com","hiredate":"2024-08-06","role":[4,5],"deptId":3,"status":1}
        //1.封装页面传递的参数
        String reqJSON = request.getReader().readLine();
        InsertUserForm insertUserForm = JSONUtil.toBean(reqJSON, InsertUserForm.class);

        //2.调用service处理
        Integer rows = userService.insert(insertUserForm);

        //3.将数据响应给前端
        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("rows", rows)));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
